import { getuser } from '../../utils/storage'
import { request, wxlogin } from '../../utils/https'
import {
  getschoolstudenthomeworkbyid
} from '../../utils/interfaces'
export default {
  name: 'Completejob',
  data () {
    return {
      homework: {
        ID: '',
        createdAt: '',
        data: {},
        schoolclazzs: '',
        star: '',
        type: '',
        uid: '',
        updatedAt: '',
        schoolstudentid: '',
        schoolhomeworkid: '',
        schoolhomework: {
          schoolhomeworkitembank: []
        }
      },
      image: '',
      index: 0,
      answer: {}
    }
  },
  async onLoad (query) {
    this.index = 0
    this.images = []
    const { ID } = await getuser('user')
    try {
      let code = await wxlogin()
      const data = await request(getschoolstudenthomeworkbyid, {
        data: {
          schoolstudentid: ID,
          ID: query.ID,
          code
        }
      })
      if (!data.data) {
        data.data = {}
      }
      this.homework = data
      console.log(data)
      wx.setNavigationBarTitle({
        title: data.schoolhomework.title
      })
    } catch (err) {
      wx.showModal({
        title: '提示',
        content: '服务器请求失败，点击确定重新加载',
        success: res => {
          if (res.confirm) {
            this.start()
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  },
  computed: {
    schoolhomeworkitembank () {
      return this.homework.schoolhomework.schoolhomeworkitembank
    }
  },
  methods: {
    key (i, k) {
      return `${i}Completejob${k}`
    }
  }
}
